package demo4;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String[] strs = new String[n];
        String str1 = scanner.next();
        String str2 = scanner.next();
        for(int i = 0; i<n; i++) {
            strs[i] = scanner.next();
        }
        int ret = Integer.MAX_VALUE;
        int index1 = -1, index2 = -1;
        for(int i = 0; i<n; i++) {
            if(strs[i].equals(str1)) {
                index1 = i;
            }
            if(strs[i].equals(str2)) {
                index2 = i;
            }
            if(index1!=-1 && index2!=-1) {
                ret = Math.min(ret, Math.abs(index1-index2));
            }
        }
        if(ret == Integer.MAX_VALUE) {
            System.out.println(-1);
        }else {
            System.out.println(ret);
        }
    }
}
